<?php
class FulltextController extends ZendX_Controller_Action
{
	private function queryES($method, $url, $parameters = array() , $data = NULL, $content_type = NULL, array $curlOptions = array())
	{
		$esconfig = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOption('ElasticSearch');
		$url = "http://" . $esconfig['host'] . "/" . $url;	
		
		$curlOptions = array_merge($curlOptions, array(
			CURLOPT_PORT=> $esconfig['port'],
			CURLOPT_TIMEOUT=>10,			
		));
		
		return parent::curl_query($method, $url, $parameters, $data, $content_type, $curlOptions);
	}
	
	public function IndexAction()
	{
//		清除全部索引
//		curl -XDELETE www.gplcn.com:9200/_all
		$this->queryES('DELETE', '_all');
		
//		建立供应商索引
//		curl -XPUT http://www.gplcn.com:9200/supplier
		$this->queryES('PUT', 'supplier');
		
//		建立供应商所以mapping
//		curl -XPUT http://www.gplcn.com:9200/supplier/default/_mapping -d'
//		{
//		    "default": {
//		        "_all": {
//		            "indexAnalyzer": "ik",
//		            "searchAnalyzer": "ik",
//		            "term_vector": "no",
//		            "store": "false"
//		        },
//		        "properties": {
//		            "companyname" : {
//		                "type": "string",
//		                "store": "no",
//		                "term_vector": "with_positions_offsets",
//		                "indexAnalyzer": "ik",
//		                "searchAnalyzer": "ik",
//		                "include_in_all": "true",
//		                "boost": 8
//		            }
//		        }
//		    }
//		}'
		$this->queryES('PUT', 'supplier/default/_mapping', array(), array(
			'default'=>array(
				'_all'=>array(
					'indexAnalyzer'=>'iK',
					'searchAnalyzer'=>'iK',
					'term_vector'=>'no',
					'store'=>false
				),
				'properties'=>array(
					'companyname'=>array(
						'type'=>'string',
						'store'=>'no',
						'term_vector'=>'with_positions_offsets',
						'indexAnalyzer'=>'iK',
						'searchAnalyzer'=>'iK',
						'include_in_all'=>true,
						'boost'=>8
					)
				)
			),			
		));
		
//		生成供应商索引数据
//		curl -XPUT http://www.gplcn.com:9200/_river/supplier/_meta -d'
//		{
//		    "type": "couchdb",
//		    "couchdb": {
//		        "host": "localhost",
//		        "port": 5984,
//		        "db": "lab",
//		        "filter": "supplier/supplier",
//		        "user": "admin",
//		        "password": "1zhxcql"
//		    },
//		    "index" : {
//		        "index" : "supplier",
//		        "type" :  "default"
//		    }
//		}'

		$this->queryES('PUT', '_river/supplier/_meta', array(), "{
    'type': 'couchdb',
    'couchdb': {
        'host': 'localhost',
        'port': 5984,
        'db': 'lab',
        'filter': 'supplier/supplier',
        'user': 'admin',
        'password': '1zhxcql'
    },
    'index' : {
        'index' : 'supplier',
        'type' :  'default'
    }
}");
		exit();
	}
}